package DAO.Impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;

import logic.Bus;
import logic.Driver;
import DAO.DriverDAO;

public class DriverDAOImpl implements DriverDAO {

	public Collection<Driver> getDriversByBus(Bus bus) {
		Session session = null;
		List<Driver> drivers = new ArrayList<Driver>();
		try {
			session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Long bus_id = bus.getId();
			drivers = (List<Driver>)session.createQuery("select d from Driver d INNER JOIN d.busses bus where bus.id = :busId ").setLong("busId", bus_id).list();
			session.getTransaction().commit();
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return drivers;
	}

}
